easySpa.use(['widget/select','widget/prompt','widget/slides']);
(function ($, window, document, undefined ){
    function use(config, callback){
        $.ajax({
            url: config.url,
            type: "get",
            dataType: 'html',
            async:false,
            success: function(data){
                if(config.type && config.type.toLowerCase() == 'css'){
                    var styleEle = document.createElement("style");
                    styleEle.innerHTML = data;
                    $('head')[0].append(styleEle);
                }else if(config.type && config.type.toLowerCase() == 'html'){
                    $(config.append).html(data);
                }
            }
        })
    }
    function useHttp(param){
        return $.ajax({
            url: param.url,
            datatype: 'json',
            data:param.data,
            contentType:'application/json; charset=utf-8',
            type: param.type,
            cache: false,
            async: false
        }).responseText;
    }

    $.fn.uploadBox = function (param){
        use({
            url:'public/common/uploadFile/uploadFile.css',
            type:'css'
        });
        use({
            url:'public/common/uploadFile/uploadFile.html',
            type:'html',
            append:param.append
        });

        //非可编辑状态添加附件按钮隐藏
        if(!param.edit){ $(".addButton").hide(); }
        //默认进来隐藏添加对话框
        //$("#addDataTr").hide();

        var fileTypeCode,fileTypes,
            fileNameRight = false,
            backResult = [],
            orderFileData = getOrderFile(),
            fileNameInp = $("#fileNameInp"),
            projectFile = $("#projectFile"),
            upload_fileType = $("#upload_fileType"),
            inputNameErrorVal = $("#inputNameErrorVal"),
            fileDescribeInp = $("#fileDescribeInp"),
            clickBtn = $("#clickBtn"),
            uploadBtn = $(".uploadBtn"),
            option = {'maxSize': 10 * 1024 * 1024};

        //判断是否可以上传
        function checkUpload(flag){
            if(flag){
                fileNameInp.css("border", "1px solid #BDBDBD");
                uploadBtn.css("background-color", "#3BAFDA");
                uploadBtn.css("border", "1px solid #3BAFDA");
                uploadBtn.css("cursor", "pointer");
                projectFile.css("cursor", "pointer");
                projectFile.attr("disabled", false);
            }else{
                fileNameInp.css("border", "1px solid #f00");
                uploadBtn.css("background-color", "#ccc");
                uploadBtn.css("border", "#ccc");
                uploadBtn.css("cursor", "not-allowed");
                projectFile.css("cursor", "not-allowed");
                projectFile.attr("disabled", true);
            }
        }

        startUpload();
        function startUpload(){
            if(param.edit){
                fileTypes = getFileType();
                fileTypeCode = fileTypes.data[0].code;
                clickBtn.attr("disabled", true);
                clickBtn.css("background-color", "#ccc");
                clickBtn.css("border-color", "#ccc");
                checkUpload(true);
                fileNameInp.val("");
                projectFile.val("");
                upload_fileType.val(fileTypes.data[0].name);
                fileDescribeInp.val("");
                $("#addDataTr").show();
            }else{
                $("#addDataTr").hide();
            }
        }

        //点击添加附件按钮显示添加对话框，清除对话框内容
        clickBtn.on("click",startUpload);

        //添加中获取文件类型
        upload_fileType.on("click", function() {
            Select.sharePool['upload_fileType'] = null;
            upload_fileType.unbind();
            selectFactory({
                data: [],
                id: "upload_fileType",
                defaultText:'',
                onSearchValueChange: function (attachEvent, data) {
                    var fileTypeValues = getCurrentFileType(data);
                    attachEvent.setData(fileTypeValues);
                },
                attrTextModel: function(name, data) {
                    fileTypeCode = getCodeByName(name, data);
                    upload_fileType.val(name);
                }
            }).open();
        });
        function getCurrentFileType(q){
            q = q ? q : '';
            var config = {
                q: q,
                orderStatus:param.orderStatus
            };
            var data = getFileType(config);
            return data;
        }
        function getCodeByName(name, data) {
            if(!data) {
                data = getFileType()
            }
            if(!name) {
                return;
            }
            data = data.data;
            for(var index = 0; index < data.length; index++) {
                var currentName = data[index].name;
                if($.trim(currentName) == $.trim(name) || $.trim(data[index].name) == $.trim(name)) {
                    return  data[index].code
                }
            }
            return "无匹配结果";
        }

        //附件数据遍历
        function forEachData(data){
            var resetData = resetDataFormat(data),fileDownloadName;
            $("#tableContainerId tbody .add-data").remove();
            if(resetData.length > 0){
                backResult.splice(0,backResult.length);
                resetData.forEach(function(value,index){
                    if(!value.fileUrl && value.path){
                        value.fileUrl = value.path
                    }
                    if(!value.fileId && value.id){
                        value.fileId = value.id
                    }
                    var fCode,backData;
                    param.system ==="tradeOrder" ?
                        backData={
                            type:value.code,
                            remark:value.desciption,
                            name:value.fileName,
                            fileId:value.id,
                            orderNo:value.orderNo
                        } :
                        backData= {
                            code:value.code,
                            desciption:value.desciption,
                            fileName:value.fileName,
                            fileId:value.id,
                            orderNo:value.orderNo
                        };
                    backResult.push(backData);
                    if(value.fileRandomName){
                        var idx = value.fileRandomName.lastIndexOf('.');
                        fCode= value.fileRandomName.substring(idx + 1, value.fileRandomName.length).toUpperCase();
                    }
                    if(value.name){
                        var idx = value.name.lastIndexOf('.');
                        fCode= value.name.substring(idx + 1, value.name.length).toUpperCase();
                    }
                    if(fCode==='DOC' || fCode=='DOCX' || fCode==='DOT' || fCode==='DOTX'){
                        value.type = 'application/msword'
                    }else if(fCode==='XLS' || fCode==='XLT' || fCode==='XLSX' || fCode==='XLTX'){
                        value.type = 'application/vnd.ms-excel'
                    }else if(fCode === 'JPG' || fCode === 'JPEG' || fCode === 'PNG' || fCode === 'BMP' || fCode === 'TIFF') {
                        value.type = 'image';
                    }else if (fCode === 'PDF'){
                        value.type = 'pdf';
                    }
                    if(!value.fileType && value.type){
                        value.fileType = value.type
                    }
                    if(value.fileName.indexOf('.')<0){
                        var idx = value.fileRandomName.lastIndexOf('.');
                        fileDownloadName = value.fileName+'.'+ value.fileRandomName.substring(idx + 1, value.fileRandomName.length);
                    }else{
                        fileDownloadName= value.fileRandomName
                    }
                    var html = '<tr class="add-data"><td >'+value.fileName+'</td>'+
                        '<td>'+value.fileTypeName+'</td>'+
                        '<td>'+value.desciption+'</td>'+
                        '<td data-fileUrl="'+value.fileUrl.toString()+'" ' +
                        ' data-fileId="'+value.fileId.toString()+'" '+
                        ' data-fileType="'+value.fileType+'" '+
                        ' data-name="'+value.fileRandomName+'" '+'';
                    if(value.type && value.type.includes("image")){
                        html += '><img class="imgFilePic" style="margin-top: 10px" src="'+value.fileUrl+'" width="50">';
                    }else if(value.type && value.type.includes("pdf")){
                        html += '><img style="margin-top: 10px;" src="../public/img/PDF.svg" width="50">';
                    }else if(value.type && value.type.includes("tiff")){
                        html += '><img style="margin-top: 10px;" src="../public/img/tiff.svg" width="50">';
                    }else if(value.type && value.type.includes("vnd")){
                        html += '><img style="margin-top: 10px;" src="../public/img/excel.svg" width="50">';
                    }else if(value.type && value.type.includes("msword")){
                        html += '><img style="margin-top: 10px;" src="../public/img/word.svg" width="50">';
                    }
                    html += '<p style="margin-top: -14px"><span class="textPreview">预览</span>';
                    if(param.edit){
                        html +='<span class="textDelete" data-status="'+value.orderStatus+'" id=' + value.id + '>删除</span>';
                    }
                    html +='<a href="'+value.fileUrl+'?filename='+fileDownloadName+'" class="textDownload" download>下载</a></p></td></tr>';
                    $("#tableContainerId tbody #addDataTr").before(html);
                });
            }
        }
        forEachData(orderFileData);

        function resetDataFormat(data){
            var rDatas = [];
            if(param.system ==="tradeOrder" && param.orderNo){
                data.forEach(function(value){
                    var rData = {id:'', code:'', desciption:'', fileName:'',
                        orderNo:'', fileId:'', fileRandomName:'', type:'',
                        orderStatus:'', fileUrl:'', fileType:'', fileTypeName:''};
                    rData.id = value.id;
                    rData.code = value.type;
                    if(!value.remark && value.desciption){
                        rData.desciption = value.desciption
                    }else if(value.remark){
                        rData.desciption = value.remark;
                    }
                    if(!value.fileName && value.name){
                        rData.fileName = value.name
                    }else{
                        rData.fileName = value.fileName;
                    }
                    rData.fileId = value.fileId;
                    rData.fileRandomName = value.fileRandomName;
                    rData.fileType = value.fileType;
                    rData.orderStatus = value.orderStatus;
                    if(!value.fileUrl && value.path){
                        rData.fileUrl = value.path
                    }else{
                        rData.fileUrl = value.fileUrl;
                    }
                    rData.fileTypeName = value.fileTypeName;
                    rDatas.push(rData);
                })
            }else{
                rDatas = data
            }
            return rDatas
        }

        //鼠标移除名称输入框进行名称校验
        fileNameInp.on("blur", checkFN);
        function checkFN(){
            fileNameRight = false;
            var fileName = fileNameInp.val(),
                reg = /^([\w\u4E00-\u9FA5_\-\s]+)+$/;
            if(reg.test(fileName)){
                if(param.orderNo) {
                    checkFileName(fileName, function (res) {
                        if (!res.data) {
                            fileNameRight = true;
                            checkUpload(false);
                            return inputNameErrorVal.val('已有该文件名！');
                        }else{
                            inputNameErrorVal.val('');
                            checkUpload(true);
                        }
                    });
                }else{
                    inputNameErrorVal.val('');
                    checkUpload(true);
                }
            }else if(fileName){
                fileNameRight = true;
                checkUpload(false);
                return inputNameErrorVal.val('请输入中英文、数字、“-_”、空格');
            }else{
                inputNameErrorVal.val('');
                checkUpload(true);
            }
        }
        //点击上传附件进行文件上传
        projectFile.on('change', function(e){
            var fileName = fileNameInp.val().trim(),
                fileDescribe = fileDescribeInp.val(),
                file = (e.srcElement || e.target).files[0],
                fileTypeName = upload_fileType.val(),
                fileUrl,fileData;

            projectFile.val("");
            if(fileNameRight){
                fileNameInp.focus();
                return;
            }
            if(!fileTypeName && !fileTypeCode){
                upload_fileType.focus();
                return $(document).promptBox({
                    isDelay: true,
                    contentDelay: "类型不能为空",
                    type: "errer",
                    time: 3000
                });
            }
            if(file == undefined){
                return false;
            }
            if (file.size > option.maxSize) {
                return $(document).promptBox({
                    isDelay: true,
                    contentDelay: '单个文件最大支持10M!',
                    type: 'errer',
                    manualClose: true,
                    time: 3000
                });
            }
            if (file.size == 0) {
                return $(document).promptBox({
                    isDelay: true,
                    contentDelay: '文件不能为空！',
                    type: 'errer',
                    manualClose: true,
                    time: 3000
                });
            }
            if(file.type.indexOf('image') != -1){
                fileUrl = '/api/v1/sys/files/upload/' + 'pic';
            }else{
                fileUrl = '/api/v1/sys/files/upload/' + 'file';
            }
            fileData = new FormData();
            fileData.append('file', file);
            $.ajax({
                type:'POST',
                url:fileUrl,
                data: fileData,
                processData: false,
                contentType: false,
                success:function(res){
                    clickBtn.attr("disabled", false);
                    clickBtn.css("background-color", "#3BAFDA");
                    clickBtn.css("border-color", "#3BAFDA");
                    if(res.errorCode === 0){
                        fileName ? fileName : fileName = res.data.name
                        res.data.fileName = fileName;
                        res.data.fileRandomName = res.data.name;
                        res.data.desciption = fileDescribe;
                        res.data.code = fileTypeCode;
                        res.data.fileTypeName = fileTypeName;
                        res.data.orderStatus = param.orderStatus;
                        res.data.orderNo = param.orderNo;
                        var config;
                        param.system ==="tradeOrder" ?
                            config={
                                type:res.data.code,
                                fileId:res.data.id,
                                name:res.data.fileName,
                                remark:res.data.desciption,
                                orderNo:res.data.orderNo
                            } :
                            config= {
                                code:res.data.code,
                                fileId:res.data.id,
                                fileName:res.data.fileName,
                                desciption:res.data.desciption,
                                orderNo:res.data.orderNo
                            };
                        if(param.orderNo) {
                            addOrderFile(JSON.stringify(config), function (rs) {
                                if (rs.errorCode === 0) {
                                    $(document).promptBox({
                                        isDelay: true,
                                        contentDelay: '添加成功！',
                                        type: 'success'
                                    });
                                    orderFileData.push(res.data);
                                    forEachData(orderFileData);
                                } else {
                                    $(document).promptBox({
                                        isDelay: true,
                                        contentDelay: rs.msg,
                                        type: "errer",
                                        time: 3000
                                    });
                                }
                            });
                        }else{
                            orderFileData.push(res.data);
                            forEachData(orderFileData);
                        }
                        $("#addDataTr").hide();
                    }else{
                        $(document).promptBox({
                            isDelay: true,
                            contentDelay: res.msg,
                            type: "errer",
                            time: 3000
                        });
                    }
                }
            });
        });
        //点击删除进行附件删除
        $("#tableContainerId tbody").delegate(".textDelete","click", function(e){
            var trIndex = $(this).parents('td').parent('tr').index();
            var currentStatus = $(this).attr('data-status');
            if(currentStatus !== param.orderStatus && param.system !=="tradeOrder"){
                return $(document).promptBox({
                    isDelay: true,
                    contentDelay: "该附件在之前状态下上传，不能删除！",
                    type: "errer",
                    time: 3000
                });
            }
            $(document).promptBox({
                title: '提示',
                type: 'warning',
                content: {
                    tip: '确认删除选中文件?'
                },
                operation: [
                    {
                        type: 'submit',
                        description: Lang.getValByKey('common', 'common_page_delete'),
                        application: 'delete',
                        operationEvent: function () {
                            delOrderFile(e.target.id, function(res){
                                orderFileData && orderFileData.splice(trIndex,1);
                                forEachData(orderFileData);
                                $(document).promptBox('closePrompt');
                                $(document).promptBox({
                                    isDelay: true,
                                    contentDelay: '删除成功！',
                                    type: 'success'
                                });
                            });
                        }
                    }
                ]
            });
        });
        //点击预览按钮进行图片预览
        $("#tableContainerId tbody").delegate(".textPreview","click", function(e){
            var tdDom = $(this).parents('td');

            var dataName = tdDom.attr('data-name'),
                dataFileUrl = tdDom.attr('data-fileUrl'),
                dataFileId = tdDom.attr('data-fileId'),
                dataFileType = tdDom.attr('data-fileType');
            var typeImageFlag,typePdfFlag,pictures = [];
            if(dataFileType.includes("image")){
                typeImageFlag = true;
                typePdfFlag=false
            }else if(dataFileType.includes("pdf")){
                typeImageFlag = false;
                typePdfFlag=true
            }else{
                return $(document).promptBox({
                    isDelay: true,
                    contentDelay: "该文件暂不支持预览！",
                    type: "errer",
                    time: 3000
                });
            }
            var pictureCfg = {
                name:dataName,
                picUrl:dataFileUrl,
                picUrlID:{
                    id:dataFileId,
                    name:dataName,
                    path:dataFileUrl,
                    type:dataFileType
                },
                typeImage:typeImageFlag,
                typePdf:typePdfFlag
            };
            pictures.push(pictureCfg);
            $('#slides').picturePreview({pictureId : dataFileId}, pictures);
        });
        //点击图片直接进行图片预览
        $("#tableContainerId tbody").delegate(".imgFilePic","click", function(e){
            var tdDom = $(this).parents('td');
            var dataName = tdDom.attr('data-name'),
                dataFileUrl = tdDom.attr('data-fileUrl'),
                dataFileId = tdDom.attr('data-fileId'),
                dataFileType = tdDom.attr('data-fileType');
            var pictures = [];
            var pictureCfg = {
                name:dataName,
                picUrl:dataFileUrl,
                picUrlID:{
                    id:dataFileId,
                    name:dataName,
                    path:dataFileUrl,
                    type:dataFileType
                },
                typeImage:true,
                typePdf:false
            };
            pictures.push(pictureCfg);
            $('#slides').picturePreview({pictureId : dataFileId}, pictures);
        });

        //获取附件信息
        function getOrderFile(){
            var params = {
                url:param.getOrderFile.url,
                type: param.getOrderFile.type,
                data:param.getOrderFile.param
            };
            var orderFileDatas = JSON.parse(useHttp(params));
            return orderFileDatas.data
        }
        //获取文件类型调用接口
        function getFileType(config){
            config ? config : config = { q: '',orderStatus:param.orderStatus};
            var params = {
                url:param.orderFileType.url,
                type: param.orderFileType.type,
                data:config
            };
            var resultData = JSON.parse(useHttp(params));
            return resultData
        }
        //校验附件名称是否重复
        function checkFileName(name,callBack){
            var config={
                orderNo:param.ckeckFileName.param.orderNo,
                fileName:name
            };
            var params = {
                url:param.ckeckFileName.url,
                type:param.ckeckFileName.type,
                data:config
            };
            var res = useHttp(params);
            callBack(JSON.parse(res));
        }
        //删除附件接口调用
        function delOrderFile(id,callBack){
            var delUrl;
            if(param.system ==="tradeOrder"){
                delUrl = '/api/v1/trd/order/files/'+param.orderNo+'/'+id+'/delete'
            }else{
                delUrl = '/api/v1/order/files/'+param.orderNo+'/'+id+'/delete'
            }
            var params = {
                url:delUrl,
                type: param.delOrderFile.type,
                data:param.delOrderFile.param
            };
            var res = useHttp(params);
            callBack(JSON.parse(res));
        }
        //添加附件接口调用
        function addOrderFile(config,callBack){
            var params = {
                url:param.addOrderFile.url,
                type: param.addOrderFile.type,
                data:config
            };
            var res = useHttp(params);
            callBack(JSON.parse(res));
        }

        //返回时隐藏添加对话框
        //$("#addDataTr").hide();
        return backResult
    }

})(jQuery, window, document);